package com.zhongc.advance.api.impl;

import com.zhongc.advance.api.UserSecurityService;
import com.zhongc.advance.common.CommonCode;
import com.zhongc.advance.common.result.Result;
import com.zhongc.advance.common.result.Results;
import com.zhongc.advance.common.result.Status;
import com.zhongc.advance.common.util.BeanUtils;
import com.zhongc.advance.common.util.Validator;
import com.zhongc.advance.info.UserSecurity;
import com.zhongc.advance.order.UserSecurityAddOrder;
import com.zhongc.advance.service.AdvanceUserSecurityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Version 1.0
 * @Author zhongc
 * @Email zhong_ch@foxmail.com
 * @History <li>Author: zhongc</li>
 * <li>Date: 2015-07-27</li>
 * <li>Version: 1.0</li>
 * <li>Content: create</li>
 */
@Service()
public class UserSecurityServiceImpl implements UserSecurityService{

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvanceUserSecurityService advanceUserSecurityService;
    @Override
    public Result addUserSecurity(List<UserSecurityAddOrder> order) {
        logger.info("增加用户安全密保问题入参 ->{}", order);
        Result result = new Result();
        try {
            Validator.validate(order);
        }catch (Exception e){
            result.setDescription(e.getMessage());
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            return result;
        }
        List<UserSecurity> list = new ArrayList<>();
        for (UserSecurityAddOrder lis : order){
            UserSecurity userSecurity = BeanUtils.copyProperties(lis,UserSecurity.class);
            list.add(userSecurity);
        }
        try {
            advanceUserSecurityService.addUserSecurity(list);
            result.setCode(CommonCode.SUCCESS);
            result.setStatus(Status.SUCCESS);
            result.setDescription("添加成功");
        }catch (DataAccessException e){
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription("添加失败");
            logger.error(e.getMessage());
        }catch (Exception e){
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription("添加失败");
            logger.error(e.getMessage());
        }
        logger.info("添加密保问题出参 result->{}",result);
        return result;
    }

    @Override
    public Result updateUserSecutity(List<UserSecurity> order) {
        logger.info("修改用户安全密保问题入参 List<UserSecurity>->{}",order);
        return null;
    }

    @Override
    public Results validateUserSecutitys(String userId) {
        logger.info("增加用户安全密保问题入参 userId ->{}",userId);
        return null;
    }
}
